import {USER_GET_DETAIL} from './constants';

import NetUtil from '../../common/NetUtil';
import config from '../../config/config';

export const getUserDetail = (token) => {
    return dispatch => {
            let url = "/s/user/info";
            let params = {
                token:token
            }
            // UIPlugin.showLoading("正在加载");
            NetUtil.postSign(config.apiUrl + url, params, function (res) {
                // UIPlugin.hideLoading();
                if (res.success) {
                    dispatch({
                        type:USER_GET_DETAIL,
                        payload:{
                            user:res.data,
                        }
                    });
                } else {
                    LoginOutPlugin.loginOutWithOutAlert();
                }
            });
    }
};


export const changeUser = (user) => {
     return dispatch => {
            dispatch({
                type:USER_GET_DETAIL,
                payload:{
                    user:user,
                }
            })
     }
}

export const clearUser = () => {
    return dispatch => {
        dispatch({
            type:USER_GET_DETAIL,
            payload:{
                user:{},
            }
        })
    }
}

export const editUser = (token,user,callback) => {
    return dispatch => {
            let url = "/s/user/update";
            let params = {
                token:token
            }

            // UIPlugin.showLoading("正在保存");

            NetUtil.postSign(config.apiUrl + url, Object.assign({},user,params), function (res) {
                // UIPlugin.hideLoading();
                if (res.success) {
                    callback();
                } else {
                    // UIPlugin.toast(res.message);
                }
            });
    }
};

export const updateNotice = (params,callBack) => {
    return dispatch => {
            let url = "/s/shop/update";
            // UIPlugin.showLoading("正在更新");
            NetUtil.postSign(config.apiUrl + url, params, function (res) {
                // UIPlugin.hideLoading();
                if (res.success) {
                    callBack();
                } else {
                    // LoginOutPlugin.loginOutWithOutAlert();
                }
        });
    }
};